package project.servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.Queries;

/**
 * Servlet implementation class Answer
 */
@WebServlet("/Answer")
public class Answer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String HOST = "smtp.gmail.com";
	private static final int PORT = 465;
	private static final String PASSWORD = "superschronisko";
	private static final String FROM = "eschronisko@gmail.com";
	
	HttpSession s;
	Queries q = new Queries();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Answer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    public static void SendAnswer(String email, String msg) throws UnsupportedEncodingException{
		/* defaultowe ustawienia serwera gmail */
		Properties props = new Properties();
		props.put("mail.smtp.host", HOST);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", PORT);
		props.put("mail.smtp.socketFactory.port", PORT);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
 
		/* Pobieranie obiektu klasy session jako instancje domyslna  - inicjalizacja sesji*/
		/* pierwszy parametr to dane naszej skrzynki - obiekt props, drugi parametr jest z klasy Authenticator*/
		/* implementuja one metode getPasswordAuthentication() zwracajaca obiekty klasy PasswordAuthentication - w celu okreslenia interfejsu zapytan o haslo */
		Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(FROM,PASSWORD);
				}
			});

		try {
			/*Message - obiekty tej klasy reprezentuj� wiadomo�ci zakodowane zgodnie ze stadardem MIME. Konstruktor przyjmuje jako parametr obiekt typu Session*/
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(FROM,"eSchronisko")); // nadawca
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); // odbiorca
			message.setSubject("eSchronisko - odpowiedz"); // temat wiadomosci
			/*tresc wiadomosci */
			message.setContent("Ponizej tresc odpowiedzi na Panskie pytanie:<br><br><br>"
					+ msg +"<br><br>Pozdrawiamy<br>Ekipa eSchronisko", "text/html; charset=utf-8");
			Transport.send(message); // wysylanie maila
			
			 
		} catch (MessagingException e) { //zg�oszenie wyj�tku
			System.err.println("Blad z wiadomoscia: " + e.getMessage());  //wypisz wyjątek
		}
	}
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String msg = request.getParameter("msg");
		String email = request.getParameter("email");
		int ID = (Integer)s.getAttribute("id_msg");
		SendAnswer(email, msg);
		s.setAttribute("sent", 1);
		q.viewMessage(ID, 1);
		response.sendRedirect("admin/new_msg.jsp");
	}

}
